package view;


import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;

import javax.swing.JPanel;
import javax.swing.JScrollPane;

import javax.swing.JTextArea;




import net.miginfocom.swing.MigLayout;

/**
 * This class manages the Filters view
 * 
 * The filter view allows the user to create new filters that can be used to
 * move particular emails into different folders
 * 
 * @author MUSAB
 * @since May 2013
 * @version $Revision: 1.0 $
 */
public class FiltersView{
	
	/**
	 * Model for the list of templates
	 */
	private DefaultListModel<String> filters;
	
	/**
	 * JList to display the templates
	 */
	private JList<String> filtersList;
	
	/**
	 * Buttons
	 */
	JButton addNewBtn, deleteBtn, editbtn, applyBtn;
	
	/**
	 * Scroll pane for templates list
	 */
	JScrollPane scrollPane;
	
	/**
	 * Body text area
	 */
	JTextArea body;
	
	/**
	 * Constructor, sets up the window
	 */
	public FiltersView(){
		
		initializeComponents();
	}

	/**
	 * Initializes the components in this view
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	private void initializeComponents() {
		
		filters = new DefaultListModel<String>();
		filtersList = new JList();
		filtersList.setModel(filters);
		
		applyBtn = new JButton("Apply", IconsManager.getImageIcon("small/tick.png"));
		addNewBtn = new JButton("Add new...", IconsManager.getImageIcon("small/plus.png"));		
		editbtn = new JButton("Edit...", IconsManager.getImageIcon("small/document-pencil.png"));
		deleteBtn = new JButton("Delete", IconsManager.getImageIcon("small/cross.png"));
		
		
		body = new JTextArea();
		body.setEditable(false);
		
		scrollPane = new JScrollPane(filtersList);
	}

	/**
	 * Creates a joption pane with the components and shows it
	 */
	public void showDialog() {
		
		// Create and Layout all the panels
		JPanel container = new JPanel(new MigLayout("fillx"));
		JPanel westPanel = new JPanel(new MigLayout("fillx"));
		container.add(westPanel, "dock west, width 80");
		
		// Adding components to the west panel
		westPanel.add(addNewBtn, "wrap, growx");
		westPanel.add(scrollPane, "growx");
		
		// Adding components to the toolbar panel
		container.add(new JLabel("Filters get applied every 10 minutes"));
		container.add(applyBtn, "push, align right");
		container.add(editbtn, "align right");
		container.add(deleteBtn, "wrap,align right");
		
		// Adding components to the main panel
		container.add(body, "growx, span 4, height 200, width 550, height 300");
		
		// Show the popup
		ViewHelpers.showDialog(container, "Manage filters");
	}

	/**
	
	 * @return the filters */
	public DefaultListModel<String> getFilters() {
		return filters;
	}

	/**
	
	 * @return the filtersList */
	public JList<String> getFiltersList() {
		return filtersList;
	}

	/**
	
	 * @return the addNewBtn */
	public JButton getAddNewBtn() {
		return addNewBtn;
	}

	/**
	
	 * @return the deleteBtn */
	public JButton getDeleteBtn() {
		return deleteBtn;
	}

	/**
	
	 * @return the editbtn */
	public JButton getEditbtn() {
		return editbtn;
	}

	/**
	
	 * @return the body */
	public JTextArea getBody() {
		return body;
	}

	/**
	 * @param body the body to set
	 */
	public void setBody(JTextArea body) {
		this.body = body;
	}

	/**
	 * returns the apply button
	
	 * @return JButton
	 */
	public JButton getApplyBtn() {
		return applyBtn;
	}
}
